package locations.dao.implement;

import java.util.List;

import javax.persistence.Query;

import locations.dao.InstitutionDAO;
import locations.model.Institution;
import locations.model.Location;

public class InstitutionDAOHibernateJPA extends
		GenericDAOHibernateJPA<Institution> implements InstitutionDAO {

	public InstitutionDAOHibernateJPA() {
		super(Institution.class);
	}

	@Override
	public List<Institution> findInstitutionsNearTo(Location location, Double delta) {
		Query q = entityManager.createQuery("Select i " +
				"from Institution i " +
				"where ((i.location.latitude  between :latitude_min  and :latitude_max) " +
				"and   ( i.location.longitude between :longitude_min and :longitude_max))")
				.setParameter("latitude_min",  location.getLatitude()  - delta)
				.setParameter("latitude_max",  location.getLatitude()  + delta)
				.setParameter("longitude_min", location.getLongitude() - delta)
				.setParameter("longitude_max", location.getLongitude() + delta);
		return (List<Institution>) q.getResultList();
	}
}

